package com.wcl.rabbit.consumer.component;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class RabbitReceive {

    @RabbitHandler
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queue-1",durable = "true"),
            exchange = @Exchange(name = "exchange-1",
                    durable = "true",
                    type = "topic",
                    ignoreDeclarationExceptions = "true"),
            key = "springboot.*"
    ))
    public void onMessage(Message<?> message, Channel channel) throws IOException {
        //接收消息后进行业务端消费处理
        System.out.println("-------------------");
        System.out.println("消费消息："+message.getPayload());

        //处理成功之后获取DeliveryTag 并进行手动ACK操作，因为配置文件中设置的是手动签收
        channel.basicAck((Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG),false);
    }


}
